<?php
abstract class NoBusinessBase_AbstractHelper_ValueReplace extends Zend_Controller_Action_Helper_Abstract
{
   protected $_replace = null;
   protected $_search = null;

   public function __construct($additiveArray = null)
   {
      if(!isset($this->_search))
      {
         $this->_search  = array(
            '{Domain}',
            '{RequestUri}',
            '%%CURRENT_USER%%',
            '%%CURRENT_GMT%%',
         );
      }

      $pRequest = $this->getRequest();
      if(!isset($this->_replace))
      {
         $this->_replace = array(
            $pRequest ? $pRequest->getHttpHost() : '',
            $pRequest ? $pRequest->getRequestUri() : '',
            $this->getUser(),
            gmdate(DateTime::ISO8601)
         );
      }
      
      if($additiveArray)
      {
         foreach($additiveArray as $key => $value)
         {
            $this->_search[] = $key;
            $this->_replace[] = $value;
         }
      }
   }

   abstract protected function getUser();

   public function direct()
   {
      return $this;
   }

   public function replaceValue($value)
   {
      return $value === null ? $value : str_replace($this->_search, $this->_replace, $value);
   }
   public function replaceValuesInArray(&$arr)
   {
      if(!is_array($arr))
         return;

      foreach($arr as $name => &$value)
      {
         if(is_array($value))
            $this->replaceValuesInArray($value);
         else
            $value = $this->replaceValue($value);
      }
   }
}

